package engine.logic.login
{
	import engine.data.ModelLocator;
	
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.net.SharedObject;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	
	import flashx.textLayout.events.ModelChange;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;
	
	import toolNet.Model;

	public class UserInfoForm implements ILoginUI
	{
		public function UserInfoForm(clip:MovieClip,container:MovieClip)
		{
			_userInfoForm=clip;
			_container=container;
		}
		
		/**登录框*/
		private var _userInfoForm:MovieClip;
		private var _container:MovieClip;
		
		private var _completeCallBack:Function;
		
		public static var IsSaveName : Boolean;
		public static var IsSavePwd : Boolean;
		
		public function set completeCallBack(cb:Function):void
		{
			_completeCallBack=cb;	
		}
		
		public function show(info:Object=null):void
		{
			//=====================================================================================
			
			_container.addChild(_userInfoForm);
			
			var rememberUserName:MovieClip=_userInfoForm.dgg1_mc;
			var rememberUserPassword:MovieClip=_userInfoForm.dgg2_mc;
			
			var userNameTxt		:TextField 		= 	_userInfoForm.uName as TextField;
			var userPassTxt		:TextField 		= 	_userInfoForm.uPass as TextField;
			
			userNameTxt.text = userPassTxt.text = "";
			
			rememberUserName		.buttonMode=	true;
			rememberUserPassword	.buttonMode	=	true;
			
			rememberUserName.gotoAndStop(1);
			rememberUserPassword.gotoAndStop(1);
			
			var localSO			:SharedObject = 	SharedObject.getLocal( "userdata" );
			
			
			if(localSO.data.userName)
			{
				userNameTxt.text	=	localSO.data.userName;
				rememberUserName.gotoAndStop(2);
				
				IsSaveName = true;
			}
			
			if(localSO.data.password)
			{
				userPassTxt.text	=	localSO.data.password;
				rememberUserPassword.gotoAndStop(2);
				
				IsSavePwd = true;
			}
			
			_userInfoForm.register.tabEnabled = false;
			//添加回车事件
			//_userInfoForm.loginBtn.addEventListener(KeyboardEvent.KEY_DOWN,StartLogin);
			//_userInfoForm.regBtn    .tabEnabled = false;		//取消注册元件的tab响应
			_userInfoForm.loginBtn	.addEventListener(MouseEvent.CLICK,loginBtnClickHandler);
			_userInfoForm.register	.addEventListener(MouseEvent.CLICK,onRegisterHandler);
			_userInfoForm.register.buttonMode = true;
			//_userInfoForm.regBtn	.addEventListener(MouseEvent.CLICK,regBtnClickHandler);
			rememberUserName		.addEventListener(MouseEvent.CLICK,rememberUNameClickHandler);
			rememberUserPassword	.addEventListener(MouseEvent.CLICK,rememberUPasswordClickHandler);
			StageInfo.stage			.focus=StageInfo.stage;//不设置焦点的话，此时焦点在上个场景的按钮上，此时已不在。需要点击场景才能使舞台获取键盘事件。
			StageInfo.stage			.addEventListener(KeyboardEvent.KEY_DOWN,loginKeyDownHandler);	
		}
		
		public function hide():void
		{
			if(_userInfoForm.parent)
			{
				_userInfoForm.parent.removeChild(_userInfoForm);
			}
			
			var rememberUserName:MovieClip=_userInfoForm.dgg1_mc;
			var rememberUserPassword:MovieClip=_userInfoForm.dgg2_mc;
			
			_userInfoForm.loginBtn	.removeEventListener(MouseEvent.CLICK,loginBtnClickHandler);
			//_userInfoForm.regBtn	.removeEventListener(MouseEvent.CLICK,regBtnClickHandler);
			rememberUserName		.removeEventListener(MouseEvent.CLICK,rememberUNameClickHandler);
			rememberUserPassword	.removeEventListener(MouseEvent.CLICK,rememberUPasswordClickHandler);
			StageInfo.stage			.removeEventListener(KeyboardEvent.KEY_DOWN,loginKeyDownHandler);	
		}

		public function recycle():void
		{
		}
		
		private function regBtnClickHandler(e:MouseEvent):void
		{
			var url:String=ModelLocator.getInstance().mainIni.getContent("loginConfig","regurl");
			
			flash.net.navigateToURL(new URLRequest(url),"_blank");
		}
		
		private function rememberUNameClickHandler(e:MouseEvent):void
		{
			var userdata:SharedObject	=	SharedObject.getLocal("userdata");
			var uPassCheckBox:MovieClip=e.currentTarget as MovieClip;
			
			if(1	==	uPassCheckBox.currentFrame	)
			{
				userdata.data.userName	=	TextField(_userInfoForm.uName).text;
				uPassCheckBox.gotoAndStop(2);
			}
			else
			{
				userdata.data.userName	=	"";
				uPassCheckBox.gotoAndStop(1);	
			}
		}
		
		private function rememberUPasswordClickHandler(e:MouseEvent):void
		{
			var userdata:SharedObject	=	SharedObject.getLocal("userdata");
			var uPassCheckBox:MovieClip=e.currentTarget as MovieClip;
			
			if(1	==	uPassCheckBox.currentFrame	)
			{
				userdata.data.password	=	TextField(_userInfoForm.uPass).text;
				uPassCheckBox.gotoAndStop(2);
			}
			else
			{
				userdata.data.password	=	"";
				uPassCheckBox.gotoAndStop(1);	
			}
		}
		
		private function loginKeyDownHandler(e:KeyboardEvent):void
		{
			if(	e.keyCode	==	Keyboard.ENTER	)
			{
				var userInfo:Object=getUserInfo();
				
				if(	""	==	userInfo.userName	||	""	==	userInfo.password)
				{
					LoginAlert.showAlert(	"UIStr_1001123"	);
					return;
				}
				
				userInfo.target=this;
				
				if(null		!=	_completeCallBack)
				{
					_completeCallBack(userInfo)
				}	
			}
		}
		
		private function loginBtnClickHandler(e:MouseEvent):void
		{
			//			var info:Object=new Object();
			//			var serverInfo:Object=getServerInfo();
			var userInfo:Object=getUserInfo();
			
			
//			if(	""	==	obj.userName	||	""	==	obj.password)
			if(	""	==	userInfo.userName	||	""	==	userInfo.password)
			{
				LoginAlert.showAlert(	StaticIni.getSwfLangStr("UIStr_1001123")	);
				return;
			}
			
			userInfo.target=this;
			//
			//			info.ip=serverInfo.ip;
			//			info.port=serverInfo.port;
			//			info.userName	=	userInfo.userName;
			//			info.password	=	userInfo.password;
			if(null		!=	_completeCallBack)
			{
				_completeCallBack(userInfo)
			}	
		}
		
		private function onRegisterHandler(evt:MouseEvent):void
		{
			var url:URLRequest = new URLRequest( ModelLocator.getInstance().mainIni.getContent( "registerUrl", "url" ) );
			navigateToURL(url,"_blank");
		}
		
		//========================================================
		/**从客户端取用户信息*/
		private function getUserInfo():Object
		{
			var obj:Object=new Object();
			
			obj.userName	=	_userInfoForm.uName.text;
			obj.password	=	_userInfoForm.uPass.text;
			
			return obj;
		}
	}
}